import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useDataStore = defineStore('data', () => {
  // State
  const patientList = ref([])
  const statisticsData = ref({
    total: 0,
    today: 0,
    week: 0,
    month: 0
  })
  const currentPage = ref(1)
  const pageSize = ref(10)
  const total = ref(0)
  const loading = ref(false)

  // Computed
  const paginatedList = computed(() => {
    const start = (currentPage.value - 1) * pageSize.value
    const end = start + pageSize.value
    return patientList.value.slice(start, end)
  })

  // Actions
  const fetchPatientList = (params = {}) => {
    return new Promise((resolve) => {
      loading.value = true
      
      // 模拟API请求
      setTimeout(() => {
        // 生成模拟数据
        const mockData = Array.from({ length: 50 }, (_, index) => ({
          id: 'P' + (index + 1).toString().padStart(6, '0'),
          name: `患者${index + 1}`,
          gender: ['男', '女'][Math.floor(Math.random() * 2)],
          age: Math.floor(Math.random() * 40 + 30),
          date: new Date(Date.now() - Math.random() * 30 * 24 * 60 * 60 * 1000)
            .toISOString().split('T')[0],
          scanType: ['胸部CT', '腹部CT'][Math.floor(Math.random() * 2)],
          vertebralLevel: ['T12', 'L1', 'L2', 'L3', 'L4', 'L5'][Math.floor(Math.random() * 6)],
          subcutaneousFat: (Math.random() * 50 + 100).toFixed(1),
          visceralFat: (Math.random() * 40 + 70).toFixed(1),
          skeletalMuscle: (Math.random() * 50 + 130).toFixed(1),
          vsfRatio: (Math.random() * 0.3 + 0.6).toFixed(2),
          status: ['已完成', '分析中', '待分析'][Math.floor(Math.random() * 3)]
        }))

        patientList.value = mockData
        total.value = mockData.length
        loading.value = false
        
        resolve(mockData)
      }, 500)
    })
  }

  const fetchStatistics = () => {
    return new Promise((resolve) => {
      // 模拟API请求
      setTimeout(() => {
        const stats = {
          total: 1234,
          today: 45,
          week: 156,
          month: 678,
          trends: {
            total: 12,
            today: 5,
            week: 8,
            month: 15
          }
        }
        
        statisticsData.value = stats
        resolve(stats)
      }, 300)
    })
  }

  const getPatientDetail = (id) => {
    return new Promise((resolve) => {
      const patient = patientList.value.find(p => p.id === id)
      resolve(patient)
    })
  }

  const deletePatient = (id) => {
    return new Promise((resolve) => {
      const index = patientList.value.findIndex(p => p.id === id)
      if (index > -1) {
        patientList.value.splice(index, 1)
        total.value -= 1
      }
      resolve(true)
    })
  }

  const exportData = (ids) => {
    return new Promise((resolve) => {
      // 模拟导出
      setTimeout(() => {
        const data = ids 
          ? patientList.value.filter(p => ids.includes(p.id))
          : patientList.value
        
        resolve(data)
      }, 500)
    })
  }

  const setCurrentPage = (page) => {
    currentPage.value = page
  }

  const setPageSize = (size) => {
    pageSize.value = size
    currentPage.value = 1
  }

  return {
    patientList,
    statisticsData,
    currentPage,
    pageSize,
    total,
    loading,
    paginatedList,
    fetchPatientList,
    fetchStatistics,
    getPatientDetail,
    deletePatient,
    exportData,
    setCurrentPage,
    setPageSize
  }
})

